<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of News
 *
 * @author Anto
 */
class News extends CI_Controller{
    public function __construct() {
            parent::__construct();
            $this->load->model('model');
            $this->load->helper('url_helper');
    }

    public function index() {
        $data['news'] = $this->model->get_news();
        $data['title'] = 'News archive';

        $this->load->view('templates/header', $data);
        $this->load->view('news/index', $data);
        $this->load->view('templates/footer');
    }
    
    

    public function view($slug = NULL) {
        $data['news_item'] = $this->model->get_news($slug);

        if (empty($data['news_item'])) {
                show_404();
        }

        $data['title'] = $data['news_item']['title'];

        $this->load->view('templates/header', $data);
        $this->load->view('news/view', $data);
        $this->load->view('templates/footer');
    }
    
    public function create() {
        $this->load->helper('form', 'url');
        $this->load->library('form_validation');
        $config['upload_path']          = './uploads/';
        $config['allowed_types']        = 'gif|jpg|png';
        $config['max_size']             = 100;
        $config['max_width']            = 1024;
        $config['max_height']           = 768;
        $this->load->library('upload', $config);

        $data['title'] = 'Create a news item';

        $this->form_validation->set_rules('title', 'Title', 'required');
        $this->form_validation->set_rules('text', 'Text', 'required');
        $this->form_validation->set_rules('cover', 'Cover Image', 'required');

        if ($this->form_validation->run() === FALSE) {
            $error = array('error' => $this->upload->display_errors());
            $this->load->view('templates/header', $data);
            $this->load->view('news/create', $error);
            $this->load->view('templates/footer');

        } else {
            $this->model->set_news();
            $this->upload->data();
            $this->load->view('news/success');
        }
    }
}
